﻿<?php
include 'connect_database.php';

set_time_limit(0);//忽略php.ini设置的脚本运行时间限制

function my_explode($delim, $str, $lim = 1)
{
    if ($lim > -2) return explode($delim, $str, abs($lim));

    $lim = -$lim;
    $out = explode($delim, $str);
    if ($lim >= count($out)) return $out;

    $out = array_chunk($out, count($out) - $lim + 1);

    return array_merge(array(implode($delim, $out[0])), $out[1]);
}

$content = $_POST["content"];

$arr1 = my_explode('.', @$_FILES["file"]["name"], -2);
	$file = '"C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs/supplement/Docs/upfile/'.@$_FILES["file"]["name"].'"';
	$sdoc = '"C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs/supplement/Docs/upfile/';
	$ddoc = '"C:/Program Files (x86)/Apache Software Foundation/Apache2.2/htdocs/supplement/Docs/';

if(!is_dir("Docs/upfile")){
   mkdir ("Docs/upfile",0777,true);
}
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<title>语文教学辅助系统</title>
		<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
		<!-- stylesheets -->
		<link rel="stylesheet" type="text/css" href="resources/css/reset.css" />
		<link rel="stylesheet" type="text/css" href="resources/css/style.css" media="screen" />
		<link rel="stylesheet" type="text/css" href="resources/css/style_fixed_full.css" />
		<link id="color" rel="stylesheet" type="text/css" href="resources/css/colors/blue.css" />
		<!-- scripts (jquery) -->
		<script src="resources/scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
		<!--[if IE]><script language="javascript" type="text/javascript" src="resources/scripts/excanvas.min.js"></script><![endif]-->
		<script src="resources/scripts/jquery-ui-1.8.custom.min.js" type="text/javascript"></script>
		<script src="resources/scripts/jquery.ui.selectmenu.js" type="text/javascript"></script>
		<script src="resources/scripts/jquery.flot.min.js" type="text/javascript"></script>
		<script src="resources/scripts/tiny_mce/jquery.tinymce.js" type="text/javascript"></script>
		<!-- scripts (custom) -->
		<script src="resources/scripts/smooth.js" type="text/javascript"></script>
		<script src="resources/scripts/smooth.menu.js" type="text/javascript"></script>
		<script src="resources/scripts/smooth.chart.js" type="text/javascript"></script>
		<script src="resources/scripts/smooth.table.js" type="text/javascript"></script>
		<script src="resources/scripts/smooth.form.js" type="text/javascript"></script>
		<script src="resources/scripts/smooth.dialog.js" type="text/javascript"></script>
		<script src="resources/scripts/smooth.autocomplete.js" type="text/javascript"></script>
		<script type="text/javascript">
			$(document).ready(function () {
				style_path = "resources/css/colors";

				$("#date-picker").datepicker();

				$("#box-tabs, #box-left-tabs").tabs();
			});
		</script>
		<style type="text/css" media="screen"> 
			html, body	{ height:100%; }
			body { margin:0; padding:0; overflow:auto; }   
			#flashContent { display:none; }
        </style> 
		<script type="text/javascript" src="js/flexpaper_flash.js"></script>
	</head>
	<body>
		
		
		<!-- header -->
		<div id="header">
			<div id="header-outer">
				<!-- logo -->
				<div id="logo">
					<h1><a href="" title="Smooth Admin"><img src="resources/images/logo.png" alt="Smooth Admin" /></a></h1>
				</div>
				<!-- end logo -->
				<!-- user -->
				<ul id="user">
					<li class="first"><a href=""></a></li>
					<li><a href="">退出</a></li>
					<li class="last highlight"><a href="">View Site</a></li>
				</ul>
				<!-- end user -->
				<div id="header-inner">
					<div id="home">
						<a href=""></a>
					</div>
					<!-- quick -->
					<ul id="quick">
					<li>
						<a href="#" title="Products"><span class="normal">课程管理</span></a>
					</li>
					<li>
						<a href="#" title="Products"><span class="icon"><img src="resources/images/icons/application_double.png" alt="Products" /></span><span>学情跟踪</span></a>
						<ul>
							<li><a href="#">班级跟踪</a></li>
							<li><a href="#">课程跟踪</a></li>
							<li>
								<a href="#">学生个体情况</a></li>
						</ul>
					</li>
					<li>
						<a href="" title="Events"><span class="icon"><img src="resources/images/icons/calendar.png" alt="Events" /></span><span>题库管理</span></a>
						
					</li>
					</ul>
					<!-- end quick -->
					<div class="corner tl"></div>
					<div class="corner tr"></div>
				</div>
			</div>
		</div>
		<!-- end header -->
		<!-- content -->
		<div id="content">
			<!-- table -->
			<div class="box">
				<!-- box / title -->
				<div class="title">
					<h5>上传文档<h5>				
				</div>
				<div style="font:bold 28px;margin:5px 10px 20px 30px;">
				<?php
				if ($_FILES["file"]["error"] > 0)	{
					echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
				}
				else	{
					if (file_exists('Docs/'.$_FILES["file"]["name"]))	{
						echo $_FILES["file"]["name"] . " already existed. ";
					}
					else	{
					
						move_uploaded_file($_FILES["file"]["tmp_name"],"Docs/upfile/" . iconv("UTF-8","GB2312",$_FILES["file"]["name"]));
						
						
					//启动OpenOffice.org服务
						exec ('C:/MyPrograms/OpenOffice.org/program/soffice -accept="socket,port=8100;urp;"');
					//convert -->pdf
						exec ('C:/MyPrograms/OpenOffice.org/program/python C:/MyPrograms/DocumentConverter.py  '.iconv("UTF-8","GB2312",$file).' '.iconv("UTF-8","GB2312",$sdoc).''.iconv("UTF-8","GB2312",$arr1[0]).'.pdf"');
					//convert  pdf -->swf
						mysql_query("INSERT INTO supplement (filename,supplement_content,flag) VALUES ('$arr1[0]','$content',0)");
						$rs = mysql_query("SELECT * FROM supplement WHERE filename='$arr1[0]'",$con);
						$row_rs = mysql_fetch_assoc($rs);
						$id = $row_rs['supplement_id'];
					
						exec ('C:/MyPrograms/SWFTools/pdf2swf -b -l -f '.iconv("UTF-8","GB2312",$sdoc).''.iconv("UTF-8","GB2312",$arr1[0]).'.pdf" -o '.iconv("UTF-8","GB2312",$ddoc).''.$id.'.swf" -T 9');
						
						if(is_file('Docs/'.$id.'.swf')==false)	{
							echo "Uploaded failed."; 
							$deleteSQL = "DELETE FROM supplement WHERE supplement_id='$id'";
							$query = mysql_query($deleteSQL);
						}
						else	{						
							echo $_FILES["file"]["name"]." is uploaded successfully.";

						}
					}
				}
				?>				
				</div>
				</br></br>
				<div style="margin:5px 10px 20px 30px;">
					<input type="button" name="upload" value="继续上传" onclick="window.location='upload_file.html'"/>
					<input type="button" name="return" value="返回列表" onclick="window.location='supplement_list.php'"/>
				</div>
			</div>	
		</div>	
		<!-- footer -->
		<div id="footer">
			<p>Copyright &copy; 2000-2010 Your Company. All Rights Reserved.</p>
		</div>
		<!-- end footert -->
	</body>
</html>